using UnityEngine;
using System.Collections;

public class BulletDestroyOnCollision : MonoBehaviour {
	
	
	public float timer = 0;
	float endTime = 1.6f;
	
	const int enemyOnePoints = 100;
	const int enemyTwoPoints = 130;
	
	// tracks initial position for limited flight distance
	float startingX;
	const float maxRange = 30;
	
	// Use this for initialization
	void Start () {
		startingX = this.transform.position.x;
	}
	// Update is called once per frame
	void Update () {
		// check location
		float dist = this.transform.position.x - startingX;
		if (dist < 0)
			dist *= -1;
		if (dist > maxRange) {
			Destroy(gameObject);
			return;
		}
		
		timer += Time.deltaTime;
		if (timer > endTime){
			Destroy(gameObject);
		}
	}
	void OnCollisionEnter(Collision collision) {
		GameObject player = GenerateObjects.getPlayer();
		if (collision.gameObject.CompareTag( "EnemyOne" )) {
			if (player != null)
				player.GetComponent<CharacterManager>().incrementScore( enemyOnePoints );
			Destroy ( collision.gameObject );
		}
		if (collision.gameObject.CompareTag( "EnemyTwo" )) {
			if (player != null) {
				player.GetComponent<CharacterManager>().incrementScore( enemyTwoPoints );
			}
			
			// increment shots
			collision.gameObject.GetComponent<TakeMuliHits>().timesShot++;
		}
		
		Destroy (gameObject);
	}
	
}
